# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 20:14:45 2012

@author: Vinicio Gomez
"""
 
from PyQt4 import QtCore, QtGui
from SaveFaces import Ui_Dialog
from RBT import Estudiante
from RBT import RedBlackTree
from PIL import Image

b = RedBlackTree()
contador=0
#The GUI is call and use, all the graphical manager is here
class SaveFace(QtGui.QDialog):  
   
  def __init__(self, c, cont, parent=None):
    global b, contador
    contador=cont
    b=c
    QtGui.QWidget.__init__(self, parent)  
    self.ui = Ui_Dialog()                                                       # New Main Window
    self.ui.setupUi(self)
    
    
    contador_s = str(contador)
    foto = '../imagen/faces/cara'+contador_s+'.jpeg'
    scene=QtGui.QGraphicsScene();
    scene.addPixmap(QtGui.QPixmap(foto))                                # Set frame
    self.ui.caraImagen.setScene(scene)
    #Connections, check events    
    QtCore.QObject.connect(self.ui.pushButton_2,
                            QtCore.SIGNAL("clicked()"), 
                            self.exit_window )   
    QtCore.QObject.connect(self.ui.pushButton,
                            QtCore.SIGNAL("clicked()"), 
                            self.next_face)    
    self.exec_()
                                                    
  def exit_window(self):
    global b
    b.Print()                                                                   # Print the RBT
    self.close()                                                                # Close Windows
  
  def next_face(self):
    global contador
    contador_s = str(contador)
    foto = '../imagen/faces/cara'+contador_s+'.jpeg'                                           #Name of the imagen
    
    nombre_completo = self.ui.lineEdit.text() +" "+ self.ui.lineEdit_2.text()
    e1 = Estudiante(self.ui.lineEdit_3.text(),nombre_completo,foto)             #Add to the tree
    b.Insert(e1)
    #Reset lineEdit´s
    self.ui.lineEdit.setText("")    
    self.ui.lineEdit_2.setText("")
    self.ui.lineEdit_3.setText("")
    #Next imagen    
    contador=contador+1
    contador_s = str(contador)
    foto = '../imagen/faces/cara'+contador_s+'.jpeg'                                           #Name of the imagen
    #foto = "cara"+contador_s+".jpeg"
    try:
        scene=QtGui.QGraphicsScene();
        scene.addPixmap(QtGui.QPixmap(foto))
        self.ui.caraImagen.setScene(scene)
        img = Image.open('foto')                                              
        #print "cara"+contador_s+".jpeg"
    except:            
        self.close()